<template>
  <el-dialog :visible.sync="visible" width="60%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>

    <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="取水户编码" prop="ecNum">
            <el-autocomplete
              style="width: 100%;"
              v-model="form.ecNum"
              :fetch-suggestions="queryEcNum"
              placeholder="请输入取水户编码"
              @select="handleEcNumSelect"
              clearable
            >
             <template slot-scope="{ item }">
              <div class="name">{{ item.ecNum }}</div>
              <span class="addr">{{ item.engName }}</span>
            </template>
          </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="取水户名称" prop="ecNumName">
            <el-input v-model="form.ecNumName" placeholder="请输入取水户名称"></el-input>
          </el-form-item>
        </el-col>
        </el-row>

        <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="取水口编码" prop="ecNumSub">
            <el-autocomplete
              v-model="form.ecNumSub"
              style="width: 100%;"
              :fetch-suggestions="queryEcNumSub"
              placeholder="请输入取水口编码"
              @select="handleEcNumSubSelect"
              clearable
            >
             <template slot-scope="{ item }">
              <div class="name">{{ item.ecNumSub }}</div>
              <span class="addr">{{ item.name }}</span>
            </template>
          </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="取水口名称" prop="ecNumSubName">
            <el-input v-model="form.ecNumSubName" placeholder="请输入取水口名称"></el-input>
          </el-form-item>
        </el-col>
        </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="行政分区" prop="areaCode">
            <el-cascader ref="areaCodeRef" v-model="form.areaCode" :options="areaTree"  :props="{expandTrigger: 'hover',label: 'name', value: 'areaCode', checkStrictly: true}" placeholder="请选择" clearable @change="handleAreaCodeChange"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="流域分区" prop="basinCode">
            <el-cascader ref = "basinCodeRef" v-model="form.basinCode" :options="basinOptions" :props="{expandTrigger: 'hover',label: 'name', value: 'basinCode', checkStrictly: true}" placeholder="请选择" clearable @change="handleBasinCodeChange"></el-cascader>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="许可用途" prop="purpose">
            <el-select
                v-model="form.purpose"
                filterable
                allow-create
                default-first-option
                placeholder="请选择">
                <el-option
                  v-for="item in purposeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="许可水量" prop="amount">
            <el-input v-model="form.amount" placeholder="请输入许可水量" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="监测日期" prop="date">
            <el-date-picker
            v-model="form.date"
            type="month"
            placeholder="请选择监测日期">
          </el-date-picker>
          </el-form-item>

        </el-col>
      </el-row>

    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {
        dictName: '',
        basinUnit: [],
        monitoringTime: ''
      },

      rules: {
        amount: [{ required: true, message: '请输入许可水量', trigger: 'blur' }],
        areaCode: [{ required: true, message: '请选择行政分区', trigger: 'blur' }],
        basinCode: [{ required: true, message: '请选择流域分区', trigger: 'blur' }],
        date: [{ required: true, message: '请选择监测日期', trigger: 'blur' }],
        ecNum: [{ required: true, message: '请输入取水户编码', trigger: 'blur' }],
        ecNumSub: [{ required: true, message: '请输入取水口编码', trigger: 'blur' }],
        purpose: [{ required: true, message: '请输入许可用途', trigger: 'blur' }],

      },
      type: '',
      purposeOptions: [
        {
          value: '服务业及其它',
          label: '服务业及其它'
        },
        {
          value: '生态和环境用水',
          label: '生态和环境用水'
        },
        {
          value: '原水供水',
          label: '原水供水'
        },
        {
          value: '制水供水',
          label: '制水供水'
        },
        {
          value: '农业农村用水及其它',
          label: '农业农村用水及其它'
        },
        {
          value: '生活用水及其它',
          label: '生活用水及其它'
        },
        {
          value: '河道内水力发电及其他',
          label: '河道内水力发电及其他'
        },
        {
          value: '工业用水及其它',
          label: '工业用水及其它'
        }
      ]
    };
  },
  computed: {
    ...mapState(["areaTree", "basinOptions"]),
  },
  methods: {
    showModal(type, row) {
      this.visible = true
      this.type = type
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "dictName": "",
          "remark": "",
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
    // 详情
    getInfoInit(id) {
      this.$api.getWaterIntakePermitInfo({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (Array.isArray(this.form.areaCode)) {
              this.form.areaCode = parseInt(this.form.areaCode.slice(-1)[0])
            }
            if (Array.isArray(this.form.basinCode)) {
              this.form.basinCode = parseInt(this.form.basinCode.slice(-1)[0])
            }
          if (this.type == 'add') {
            this.$api.addWaterIntakePermit(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateWaterIntakePermit(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
    async checkDuplicate() {
      const ecNumSub = this.form.ecNumSub;
      if (!ecNumSub) return;
      // 假设你的后台接口是这样
      const response = await this.$api.checkWaterIntake({ ecNumSub: ecNumSub });
      if (!response.data) {  //重复时
        this.form.ecNumSub = ''
      }
    },
    //查询取水户编码下拉框
    async queryEcNum(queryString, cb) {
      const response = await this.$api.getEcNums({ keywords: queryString });
      if(response.code == 200) {
        cb(response.data)
      }
    },
    async handleEcNumSelect(item) {
      this.form.ecNum = item.ecNum;
      this.form.ecNumName = item.engName;
    },

    //查询取水口编码下拉框
    async queryEcNumSub(queryString, cb) {
      const response = await this.$api.getEcNumSubs({ keywords: queryString });
      console.log('response: ', response);
      if(response.code == 200) {
        cb(response.data)
      }
    },
    async handleEcNumSubSelect(item) {
      this.form.ecNumSub = item.ecNumSub;
      this.form.ecNumSubName = item.name;
    },
    handleAreaCodeChange(val){
            this.$refs.areaCodeRef.dropDownVisible = false;
          },

          handleBasinCodeChange(val){
            this.$refs.basinCodeRef.dropDownVisible = false;
          },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
.name{
  font-weight: bold;
}
.addr{
  color: #7d7d7d;
  font-size: small;
}
</style>
